package minderupt.selenate.cli;

import org.apache.commons.cli.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 */
public class CommonsCLIGlobalOptions implements GlobalOptions {

    private static Log LOGGER = LogFactory.getLog(CommonsCLIGlobalOptions.class);
    private Options options;

    private boolean importFlag;
    private String fileToImport;

    private String script;
    private String scripts;

    private String rcHost;
    private int rcPort;
    private String rcStartup;
    private String rcInitialUrl;

    private String reportLocation;


    public CommonsCLIGlobalOptions(String[] args) {

        defineOptions();
        parseCommandLine(args);

    }


    private void parseCommandLine(String[] args) {

        CommandLineParser cmdLineParser = new GnuParser();
        CommandLine cmdLine = null;
        try {
            cmdLine = cmdLineParser.parse(this.options, args, false);
        } catch(Exception e) {
            LOGGER.fatal("Unable to parse command-line arguments.", e);
            System.exit(1);
        }

        // scripts
        if(cmdLine.hasOption(CLI_SCRIPT)) setScript(cmdLine.getOptionValue(CLI_SCRIPT));
        if(!cmdLine.hasOption(CLI_SCRIPT) && cmdLine.hasOption(CLI_SCRIPTS)) setScripts(cmdLine.getOptionValue(CLI_SCRIPTS, "."));

        // selenium remote control
        setRcHost(cmdLine.getOptionValue(CLI_REMOTE_CONTROL_HOST, "localhost"));
        setRcPort(Integer.parseInt(cmdLine.getOptionValue(CLI_REMOTE_CONTROL_PORT, "4444")));
        setRcStartup(cmdLine.getOptionValue(CLI_REMOTE_CONTROL_STARTUP, "*firefox"));
        setRcInitialUrl(cmdLine.getOptionValue(CLI_REMOTE_CONTROL_INITIAL_URL, "http://localhost/"));

        // reporting
        setReportLocation(cmdLine.getOptionValue(CLI_REPORT_LOCATION, "./report.html"));

        // import flag
        if(cmdLine.getOptionValue(CLI_IMPORT, "").equals("")) {
            setImport(false);
        } else {
            setImport(true);
            setScript(cmdLine.getOptionValue(CLI_IMPORT));
        }




    }

    private void defineOptions() {


        this.options = new Options();

        this.options.addOption(new Option(CLI_SCRIPT, true, "Path to the individual script to execute. (DEFAULT: none)"));
        this.options.addOption(new Option(CLI_SCRIPTS, true, "Path to the base directory containing scripts to execute. (DEFAULT: .)"));

        this.options.addOption(new Option(CLI_REMOTE_CONTROL_HOST, true, "Host name or IP address of the Selenium Remote Control. (DEFAULT: localhost)"));
        this.options.addOption(new Option(CLI_REMOTE_CONTROL_PORT, true, "Port of the Selenium Remote Control. (DEFAULT: 4444)"));
        this.options.addOption(new Option(CLI_REMOTE_CONTROL_STARTUP, true, "Browser startup command, i.e. *firefox or *safari. (DEFAULT: *firefox)"));
        this.options.addOption(new Option(CLI_REMOTE_CONTROL_INITIAL_URL, true, "The base URL in which we should start our tests. (DEFAULT: http://localhost/)"));

        this.options.addOption(new Option(CLI_REPORT_LOCATION, true, "Location where to create results report."));
        this.options.addOption(new Option(CLI_IMPORT, true, "Source file to import."));

    }

    @Override
    public boolean isImport() {
        return importFlag;
    }

    @Override
    public void setImport(boolean importFlag) {
        this.importFlag = importFlag;
    }

    @Override
    public String getFileToImport() {
        return fileToImport;
    }

    @Override
    public void setFileToImport(String fileToImport) {
        this.fileToImport = fileToImport;
    }

    @Override
    public String getReportLocation() {
        return reportLocation;
    }

    @Override
    public void setReportLocation(String reportLocation) {
        this.reportLocation = reportLocation;
    }

    @Override
    public String getScript() {
        return (this.script);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setScript(String script) {
        this.script = script;
    }


    @Override
    public String getScripts() {
        return this.scripts;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setScripts(String scripts) {
        this.scripts = scripts;
    }

    @Override
    public String getRcHost() {
        return rcHost;
    }

    @Override
    public void setRcHost(String rcHost) {
        this.rcHost = rcHost;
    }

    @Override
    public int getRcPort() {
        return rcPort;
    }

    @Override
    public void setRcPort(int rcPort) {
        this.rcPort = rcPort;
    }

    @Override
    public String getRcStartup() {
        return rcStartup;
    }

    @Override
    public void setRcStartup(String rcStartup) {
        this.rcStartup = rcStartup;
    }

    @Override
    public String getRcInitialUrl() {
        return rcInitialUrl;
    }

    @Override
    public void setRcInitialUrl(String rcInitialUrl) {
        this.rcInitialUrl = rcInitialUrl;
    }
}
